package com.action;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import net.sf.jasperreports.engine.export.JRXlsExporterParameter;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.springframework.beans.factory.annotation.Autowired;

import com.entity.Customer;
import com.entity.Department;
import com.entity.Person;
import com.opensymphony.xwork2.ActionSupport;
import com.service.CustomerService;
import com.service.DepartmentService;
import com.service.PersonService;

@ParentPackage("jasperreports-default")
public class ExportAction extends ActionSupport implements ServletResponseAware{
	@Autowired
	private PersonService personService;
	@Autowired
	private DepartmentService departmentService;
	@Autowired
	private CustomerService customerService;
	private List<Person> persons;
	private Department department;
	private Person person;
	public Customer getCustomer() {
		return customer;
	}

	public void setCustomer(Customer customer) {
		this.customer = customer;
	}

	public List<Customer> getCustomers() {
		return customers;
	}

	public void setCustomers(List<Customer> customers) {
		this.customers = customers;
	}

	private Customer customer;
	private List<Customer> customers;
	private Map<Object, Object> map = new HashMap<Object, Object>();

	private String fileName;
	private HttpServletResponse response; 
	public HttpServletResponse getResponse() {
		return response;
	}

	public void setResponse(HttpServletResponse response) {
		this.response = response;
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public Map<Object, Object> getMap() {
		return map;
	}

	public void setMap(Map<Object, Object> map) {
		this.map = map;
	}

	public Department getDepartment() {
		return department;
	}

	public void setDepartment(Department department) {
		this.department = department;
	}

	public Person getPerson() {
		return person;
	}

	public void setPerson(Person person) {
		this.person = person;
	}

	public List<Person> getPersons() {
		return persons;
	}

	public void setPersons(List<Person> persons) {
		this.persons = persons;
	}

	public List<Department> getDepartments() {
		return departments;
	}

	public void setDepartments(List<Department> departments) {
		this.departments = departments;
	}

	List<Department> departments;

	/**
	 * map.put(JRXlsmapParameter.CHARACTER_ENCODING, "UTF-8");
	 * map.put(JRXlsmapParameter.OUTPUT_STREAM, httpOut);
	 * map.put(JRXlsmapParameter.IS_DETECT_CELL_TYPE, Boolean.TRUE); //
	 * 设置SheetName。 map.put(JRXlsmapParameter.SHEET_NAMES, sheetNameArray); //
	 * 用于防止数据过多时出现两行内容 map.put(JRXlsmapParameter.IS_COLLAPSE_ROW_SPAN,
	 * Boolean.TRUE); map.put(JRXlsmapParameter.JASPER_PRINT_LIST,
	 * reportDataList); map.put(JRXlsmapParameter.IS_ONE_PAGE_PER_SHEET,
	 * Boolean.FALSE); map.put(JRXlsmapParameter.IS_WHITE_PAGE_BACKGROUND,
	 * Boolean.FALSE); map.put(JRXlsmapParameter.IS_DETECT_CELL_TYPE,
	 * Boolean.TRUE); map.put(JRXlsmapParameter.
	 * IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE); //
	 * 设置单元格可以自定义（在ireport中画）合并。 map.put(JRXlsmapParameter.IS_COLLAPSE_ROW_SPAN,
	 * Boolean.FALSE);
	 * 
	 * @Description: TODO(简要描述方法的作用)
	 * @return
	 */
	@Action(value = "export-per", results = { @Result(name = "success", location = "jasper/person.jasper", type = "jasper", params = {
			"dataSource", "persons", "format", "XLS","exportParameters", "map" }) })
	public String exportPersons() {

		try {
			fileName = new String("人员名单.xls".getBytes("gbk"), "ISO8859-1");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		initMap();
		persons = personService.getAllPersons(null, person);
		response.setHeader("Content-Disposition", "inline;filename=" + fileName); 
		return "success";
	}

	private void initMap() {
		map.put(JRXlsExporterParameter.CHARACTER_ENCODING, "UTF-8");
		// map.put(JRXlsExporterParameter.OUTPUT_STREAM,"");
		map.put(JRXlsExporterParameter.IS_DETECT_CELL_TYPE, Boolean.TRUE);
		// 设置SheetName。
		// map.put(JRXlsExporterParameter.SHEET_NAMES, "");
		// 用于防止数据过多时出现两行内容
		map.put(JRXlsExporterParameter.IS_COLLAPSE_ROW_SPAN, Boolean.TRUE);
		// map.put(JRXlsExporterParameter.JASPER_PRINT_LIST, reportDataList);
		map.put(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE);
		map.put(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE);
		map.put(JRXlsExporterParameter.IS_DETECT_CELL_TYPE, Boolean.TRUE);
		map.put(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS,
				Boolean.TRUE);
		// 设置单元格可以自定义（在ireport中画）合并。
		map.put(JRXlsExporterParameter.IS_COLLAPSE_ROW_SPAN, Boolean.FALSE);

	}

	@Action(value = "export-dep", results = { @Result(name = "success", location = "jasper/dep.jasper", type = "jasper", params = {
			"dataSource", "departments", "format", "XLS","exportParameters", "map" }) })		
	public String exportDepartments() {
		try {
			fileName = new String("部门.xls".getBytes("gbk"), "ISO8859-1");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		initMap();
		departments = departmentService.getAllDepartments(null, department);
		response.setHeader("Content-Disposition", "inline;filename=" + fileName); 
		return "success";
	}
	@Action(value = "export-cus", results = { @Result(name = "success", location = "jasper/cus.jasper", type = "jasper", params = {
			"dataSource", "customers", "format", "XLS","exportParameters", "map" }) })		
	public String exportCustomers() {
		try {
			fileName = new String("客户名单.xls".getBytes("gbk"), "ISO8859-1");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		initMap();
		customers = customerService.getAllCustomers(null, customer);
		response.setHeader("Content-Disposition", "inline;filename=" + fileName); 
		return "success";
	}
	@Override
	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
		
	}
}
